import re


def reMatch():
    text = "Itcast.cn"
    # 普通匹配
    result = re.match("Itcast", text)
    group = result.group()
    print(group)  # itcast

    # 忽略大小写匹配
    result = re.match("itcast", text, re.I)
    group = result.group()
    print(group)  # itcast
    print(group[0:5])  # Itcas

    print(re.match('.', "MY").group())  # M
    print(re.match('t.o', 'too').group())  # too
    print(re.match('h', "hello python").group())  # h
    print(re.match('[hH]', 'Helllo').group())  # H
    print(re.match("[hH]ello", "Hello").group())  # Hello
    # 匹配0-9的多种写法
    print(re.match("[0123456789]Hello", "7Hello").group())  # 7Hello
    print(re.match("[0-9]Hello", "7Hello").group())  # 7Hello
    # 匹配0-3和5-9
    print(re.match("[0-35-9]Hello", "7Hello").group())  # 7Hello
    # 匹配数字
    print(re.match(r"嫦娥\d号", "嫦娥1号发射成功").group())  # 嫦娥1号
    # 匹配非数字
    print(re.match(r"嫦娥\D号", "嫦娥哈号").group())  # 嫦娥哈号


# re.search
def reSearch():
    text = "Hello My Bro"
    match = re.search(r'My', text)
    if match:
        print("匹配成功")
    else:
        print("匹配失败")


# re.sub
def reSub():
    text = "version=1.2.3 haha"
    # re.sub()它接受三个参数：正则表达式模式、替换字符串和输入文本
    # 下面表示匹配以version=开头的内容，并将后面的内容作为第二个组捕获。然后，我们将匹配到的内容替换为'\g<1>4.5.6'，其中\g<1>表示第一个组（即version=），然后拼接上新的版本号
    new_text = re.sub(r'(version=)(.*)', r'\g<1>4.5.6', text)
    print(new_text)


if __name__ == '__main__':
    reMatch()
